package cm.server.controller;

import cm.server.dao.SensorLogRepository;
import cm.server.entity.SensorLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: rainforcetime
 * @Date: 2021/4/8 20:11
 */

@RestController
@CrossOrigin
@RequestMapping("sensorlog")
public class SensorLogController {
    @Autowired
    private SensorLogRepository sensorLogRepository;

    @RequestMapping(value = "/period")
    /**
     *@describtion 获取给定时间段中每个区域的人数
     *@author zy
     *@params 起始时间，结束时间，日期
     *@return {sid, peopleNum}
     *@createTime 2021/4/12 17:07
     */
    public Map<Integer, Integer> getAreaDenByTime(int startTime, int endTime, int day) {
        List<SensorLog> logs = sensorLogRepository.findAllByPeriod(startTime, endTime, day);
        Map<Integer, Integer> sensors = new HashMap<>();
        for (int i = 0; i < logs.size(); ++i) {
            int sid = logs.get(i).getSid();
            if (sensors.containsKey(sid)) {
                sensors.put(sid, sensors.get(sid) + 1);
            } else {
                sensors.put(sid, 1);
            }
        }
        return sensors;
    }

    @RequestMapping(value = "/userTrack")
    /**
     *@describtion 获取用户的真实轨迹
     *@author zy
     *@params [user_id, day]
     *@return java.util.List<java.util.List < java.lang.Integer>> [time, sid, residence]
     *@createTime 2021/4/13 10:05
     */
    public List<List<Integer>> userRealTrack(int user_id, int day) {
        List<SensorLog> logs = sensorLogRepository.findAllById(user_id, day);
        int length = logs.size();
        List<List<Integer>> tracks = new ArrayList<>();
        for (int i = 0; i < length; ++i) {
            List<Integer> track = new ArrayList<>();
            track.add(logs.get(i).getTime());
            track.add(logs.get(i).getSid());
            if (i < length - 1) {
                int next_sid = logs.get(i + 1).getSid();
                Boolean flag = false;
                if (next_sid > 19999) {
                    flag = true;
                    int time = 0;
                    while (next_sid > 19999) {
                        time += logs.get(i + 1).getTime() - logs.get(i).getTime();
                        i++;
                        next_sid = logs.get(i).getSid();
                    }
                    track.add(time);
                }
                if (flag == false) {
                    track.add(logs.get(i + 1).getTime() - logs.get(i).getTime());
                }
            }
            tracks.add(track);
        }
        return tracks;
    }

    @RequestMapping(value = "peoplePosition", method = RequestMethod.GET)
    public List<SensorLog> peoplePosition(int startTime, int endTime, int day) {
        List<SensorLog> pList = sensorLogRepository.findAllByLimitTime(startTime, endTime, day);
        System.out.println("人数：" + pList.size());
        int length = pList.size();
        pList = pList.stream().filter(num -> {
            return num.getSid() < 20000;
        }).collect(Collectors.toList());

        pList = pList.stream().filter(num -> {
            return num.getSid() < 20000;
        }).collect(Collectors.toList());
        return pList;
    }
}
